<!doctype html public "-//w3c//dtd html 4.0 transitional//en">


<!-- WARNING! This file is generated. -->
<!-- To alter documentation, edit files in src directory -->


<html><head>
<title>Put Test Code in Same Package</title>
<link rel="stylesheet" href="utplsql.css" content="text/css">
<meta name="keywords" content="utPLSQL, PL\SQL, Unit Testing, Framework, Oracle"/>
<meta name="description" content="Unit Testing PL\SQL"/>
<meta name="title" content="Put Test Code in Same Package"/>
<meta name="author" content="Steven Feuerstein, Chris Rimmer, Patrick Barel"/>
<meta name="copyright" content="(C) 2000-2005 Steven Feuerstein, Chris Rimmer, Patrick Barel"/>
</head><body>
<div class="purple_bar"><a href="index.html"><img src="utplsql.jpg" border=0></a></div>
<p>[ <A href="index.html">Home</A>
 | <A href="started.html">Getting Started</A>
 | <A href="buildpack.html">Build Test Packages</A>
 | <A href="examples.html">Examples</A>
 | <A href="userguide.html">User Guide</A>
 | <A href="release.html">Release Notes</A>
 | <A href="map.html">Document Map</A> ]</p>
<p><A href="testapi.html">&lt; Previous Section: Test an Entire Package API</A> | <A href="prefix.html">Next Section: Use Non-Default Prefix &gt;</A></p>
<!-- Begin utPLSQL Body -->
<!-- $Id: samepack.html,v 1.3 2002/07/25 12:16:44 chrisrimmer Exp $ -->
<h1>
Put Test Code in Same Package</h1>

<p>In some cases (usually when your packages are small and the code you
need to write to construct your tests is also constrained), you will not
want to bother with creating a separate package to test your code. To do
this, you will put the setup, teardown and unit test procedures inside
the package specification and body. We look at two examples:
<ul>
<li>
<a href="#str">Testing a simple string function</a></li>
<li>
<a href="#coll">Testing the population of a collection</a></li>
</ul>

<h3>
   <a name="str">   
      Testing a simple string function</a></h3>
Suppose I have my basic sting package, containing (for now at least) just
a single function:
<pre>/*file str.pks */
CREATE OR REPLACE PACKAGE str
IS
   FUNCTION betwn (
      string_in IN VARCHAR2,
      start_in IN PLS_INTEGER,
      end_in IN PLS_INTEGER
   )
      RETURN VARCHAR2;
END str;
/</pre>
Now it is time to test the function. I really don't want to bother with
a separate package; let's keep it together. To do this, I change the specification
to:
<pre>CREATE OR REPLACE PACKAGE str
IS
   FUNCTION betwn (
      string_in IN VARCHAR2,
      start_in IN PLS_INTEGER,
      end_in IN PLS_INTEGER
   )
      RETURN VARCHAR2;
      
   PROCEDURE ut_setup;
   PROCEDURE ut_teardown;
 
   -- For each program to test...
   PROCEDURE ut_betwn;
      
END str;
/</pre>
The package body contains nothing unusual; it is the same test for str.betwn
that you can find in the <a href="testfunc.html#ScalarFunction">Testing
a Scalar Function</a> example. But when I execute my test, I need to tell
utPLSQL that my test code is located in the same package:
<pre>SQL&gt; exec utconfig.showconfig
=============================================================
utPLSQL Configuration for SCOTT
   Directory: e:\openoracle\utplsql\utinstall\examples
   Autcompile? Y
   Manual test registration? N
   Prefix = ut_
=============================================================

PL/SQL procedure successfully completed.

SQL&gt; exec utPLSQL.test ('str', samepackage_in =&gt; TRUE)
.
&gt;    SSSS   U     U   CCC     CCC   EEEEEEE   SSSS     SSSS
&gt;   S    S  U     U  C   C   C   C  E        S    S   S    S
&gt;  S        U     U C     C C     C E       S        S
&gt;   S       U     U C       C       E        S        S
&gt;    SSSS   U     U C       C       EEEE      SSSS     SSSS
&gt;        S  U     U C       C       E             S        S
&gt;         S U     U C     C C     C E              S        S
&gt;   S    S   U   U   C   C   C   C  E        S    S   S    S
&gt;    SSSS     UUU     CCC     CCC   EEEEEEE   SSSS     SSSS
.
 SUCCESS: "str"</pre>

<h3><a name="coll">
      Testing the population of a collection</a></h3>
Collections are very useful structures, but they can be difficult to analyze
and compare. utPLSQL provides the utAssert.eqColl and utAssert.eqCollAPI
programs to help you do this.
<p>For this example, consider the fileIO package: it implements a path
feature for the UTL_FILE package. In other words, you request to open a
file and your file-opening program will search through each of the directories
in the path in sequence until it finds the file or exhausts the list. Here
is the specification of this package:
<pre>/*file filepath1.pkg */
CREATE OR REPLACE PACKAGE fileIO
IS
   c_delim CHAR(1) := ';';
   
   dirs dirs_tabtype := dirs_tabtype ();
   
   -- Unit test list
   ut_dirs dirs_tabtype := dirs_tabtype ();
   
   PROCEDURE setpath (str IN VARCHAR2, delim IN VARCHAR2 := c_delim);
   FUNCTION path RETURN VARCHAR2;
   FUNCTION pathlist RETURN dirs_tabtype;

   FUNCTION open (file IN VARCHAR2, loc IN VARCHAR2 := NULL) RETURN UTL_FILE.FILE_TYPE;
   
   -- Unit test code in same package
   PROCEDURE ut_setup;
   PROCEDURE ut_teardown;
   PROCEDURE ut_setpath;
END;
/</pre>
A few things to notice about this package:
<ul>
<li>
It declares a publicly available collection, fileIO.dirs, in which the
path is deposited. Because it is declared in the package specification,
I can use the utAssert.eqColl procedure (if the collection was hidden in
the package body, I would have to use utAssert.eqCollAPI).</li>

The test code is in the same package: the setup,
teardown and test pogram for fileIO.setpath.

I declare a second, publicly available collection,
fileIO.ut_dirs, against which I will compare the path that is populated
by fileIO.setpath.
</ul>
Given that, let's take a look at the implementation of the test program:
<pre><a name="useseqcoll"></a>PROCEDURE ut_setpath
IS
BEGIN
   /* Populate base collection */
   ut_dirs.DELETE;
   
   ut_dirs.EXTEND(2);
   ut_dirs(1) := 'c:\temp';
   ut_dirs(2) := 'e:\demo';
   
   /* Call setpath to do the work */
   setpath ('c:\temp;e:\demo');
   
   utAssert.eqColl (
      'Valid double entry',
      'fileio.dirs',
      'fileio.ut_dirs'
      );
END;</pre>
This program consists of three steps:
<blockquote>
<li>
Populate the test collection with direct assignments. Call the setPath
program to populate the actual collection (fileIO.dirs). Call the assertion
program to compare the two. Notice that I pass the <i>names</i> of the
collections to the assertion program. utAssert uses dynamic SQL to build
a PL/SQL block "on the fly" that compares values from the collections.</li>
</blockquote>

<!-- End utPLSQL Body -->
<p><A href="testapi.html">&lt; Previous Section: Test an Entire Package API</A> | <A href="prefix.html">Next Section: Use Non-Default Prefix &gt;</A></p>
<div class="purple_bar"><a href="index.html"><img src="utplsql.jpg" border=0></a></div>
<p class="copyright">Copyright (C) 2000-2005 <A href="mailto:steven@stevenfeuerstein.com">Steven Feuerstein<A>, <A href="mailto:c@24.org.uk">Chris Rimmer<A>, <A href="mailto:pbarel@vda.nl">Patrick Barel<A> All rights reserved</p>
</body></html>